﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Threading;

namespace FindDuplicateFile
{
    public class WorkInProgress : IWorkInProgress
    {
        public WorkInProgress(Window _Wnd,Label _lbl, ProgressBar _pb)
        {
            Wnd = _Wnd;
            lbl = _lbl;
            pb = _pb;

            lbl.Visibility = Visibility.Collapsed;
            pb.Visibility = Visibility.Collapsed;
        }

        public void BeginWork(string sWorkTitle, int? NbStep = null)
        {
            Wnd.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
            {
                lbl.Content = sWorkTitle != null ? sWorkTitle : string.Empty;

                lbl.Visibility = Visibility.Visible;
                pb.Visibility = Visibility.Visible;

                if (NbStep.HasValue == true)
                {
                    pb.IsIndeterminate = false;
                    pb.Minimum = 0.0;
                    pb.Maximum = (double)NbStep;
                    pb.Value = 0.0;
                }
                else
                {
                    pb.IsIndeterminate = true;
                }
            }));
        }

        public void AdvanceWork(string sStepTitle, int? iStep)
        {
            Wnd.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
            {
                if (sStepTitle != null)
                    lbl.Content = sStepTitle;

                if (iStep.HasValue == true)
                    pb.Value += (double)iStep;
            }));
        }

        public void AdvanceWork(string sStepTitle = null)
        {
            AdvanceWork(sStepTitle, null);
        }

        public void AdvanceWork(int iStep)
        {
            AdvanceWork(null, iStep);
        }

        public void EndWork(string sWorkTitle)
        {
            Wnd.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
            {
                lbl.Content = sWorkTitle != null ? sWorkTitle : string.Empty;

                if (pb.IsIndeterminate == false)
                    pb.Value = pb.Maximum;

                lbl.Visibility = Visibility.Collapsed;
                pb.Visibility = Visibility.Collapsed;
            }));
        }

        public Window Wnd     {get; set;}
        public Label lbl      {get; set;}
        public ProgressBar pb {get; set;}
    }
}
